//
//  JSMDreamCityPrefectureTrailsDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/3.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMDreamCityPrefectureTrailsDetailsViewController.h"
#import "JSMDreamCityPrefectureTrailsArrayModel.h"
#import "JSMDreamCityPrefectureTrailsArrayTableViewCell.h"
#import "JSMDreamCityPrefectureTrailsPicturesModel.h"
#import "JSMDreamCityPrefectureImagesViewController.h"

#import "ShareView.h"
@interface JSMDreamCityPrefectureTrailsDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property(nonatomic, strong) BaseTableView *myTableView;
@property(nonatomic, strong) BaseImageView *myImageView;
@property(nonatomic, strong) JSMDreamCityPrefectureTrailsArrayModel *dreamCityPrefectureTrailsArrayModel;
@property(nonatomic, strong) NSArray *tempArray;
@property(nonatomic, strong) BaseScrollView *animateView;
@property(nonatomic, strong) NSArray *scrollImageArray;

@property(nonatomic, strong) BaseButton *moreImagesButton;
@property(nonatomic, strong) BaseButton *collectButton;
@property(nonatomic, strong) BaseButton *shareButton;
/**
 *  分享标题
 */
@property (nonatomic, strong) NSString *shareTitle;

/**
 *  分享内容
 */
@property (nonatomic, strong) NSString *shareText;

/**
 *  分享链接地址
 */
@property (nonatomic, strong) NSString *shareUrl;
@property(nonatomic, strong) ShareView *shareView;



@end

@implementation JSMDreamCityPrefectureTrailsDetailsViewController

#pragma mark - Method viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestDataFromNetWorking];
}
#pragma mark - Method 创建专区旅游详情图片视图
- (BaseTableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _myTableView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_myTableView];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    }
    return _myTableView;
}
- (BaseImageView *)myImageView {
    if (!_myImageView) {
        _myImageView = [[BaseImageView alloc] init];
        _myImageView.clipsToBounds = YES;
        _myImageView.userInteractionEnabled = YES;
        _myImageView.frame = CGRectMake(0, -200, SCREEN_WIDTH, 200);
        [self.myTableView addSubview:_myImageView];
        _myImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _myImageView;
}

#pragma mark - Method 创建旅游路线按钮
- (BaseButton *)moreImagesButton {
    if (!_moreImagesButton) {
        _moreImagesButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.myImageView addSubview:_moreImagesButton];
        _moreImagesButton.backgroundColor = [UIColor blackColor];
        _moreImagesButton.alpha = 0.8;
        _moreImagesButton.layer.cornerRadius = 30*ALL_EDITION_WIDTH/2;
        [_moreImagesButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.myImageView.mas_bottom).with.offset(-20*ALL_EDITION_HEIGHT);
            make.right.equalTo(self.collectButton.mas_left).with.offset(-10*ALL_EDITION_WIDTH);
            make.width.mas_equalTo(30*ALL_EDITION_WIDTH);
            make.height.mas_equalTo(30*ALL_EDITION_WIDTH);
        }];
        _moreImagesButton.layer.borderWidth = 2;
        _moreImagesButton.layer.borderColor = [[UIColor whiteColor] CGColor];
        [_moreImagesButton addTarget:self action:@selector(moreImageAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreImagesButton;
}
#pragma mark - Method 收藏按钮
- (BaseButton *)collectButton {
    if (!_collectButton) {
        _collectButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.myImageView addSubview:_collectButton];
        _collectButton.backgroundColor = [UIColor blackColor];
        _collectButton.alpha = 0.8;
        _collectButton.layer.borderWidth = 2;
        _collectButton.layer.borderColor = [[UIColor whiteColor] CGColor];
        _collectButton.layer.cornerRadius = 30*ALL_EDITION_WIDTH/2;
        [_collectButton addTarget:self action:@selector(collectMessageButton:) forControlEvents:UIControlEventTouchUpInside];
        [_collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.myImageView.mas_bottom).with.offset(-20*ALL_EDITION_HEIGHT);
            make.right.equalTo(self.shareButton.mas_left).with.offset(-10*ALL_EDITION_WIDTH);
            make.width.mas_equalTo(30*ALL_EDITION_WIDTH);
            make.height.mas_equalTo(30*ALL_EDITION_WIDTH);
        }];
        
        if ([SaveTool isHaveModelInPlist:self.tempModel type:SaveDreamCityPrefectureModel]) {
            [_collectButton setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
        } else {
            [_collectButton setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
        }
    }
    return _collectButton;
}
- (BaseButton *)shareButton {
    if (!_shareButton) {
        // BarButtonRight -> 分享
        _shareButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.myImageView addSubview:_shareButton];
        _shareButton.backgroundColor = [UIColor blackColor];
        _shareButton.alpha = 0.8;
        _shareButton.layer.cornerRadius = 30*ALL_EDITION_WIDTH/2;
        [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.myImageView.mas_bottom).with.offset(-20*ALL_EDITION_WIDTH);
            make.right.equalTo(self.view.mas_right).with.offset(-20*ALL_EDITION_WIDTH);
            make.width.mas_equalTo(30*ALL_EDITION_WIDTH);
            make.height.mas_equalTo(30*ALL_EDITION_WIDTH);
        }];
        _shareButton.layer.borderWidth = 2;
        _shareButton.layer.borderColor = [[UIColor whiteColor] CGColor];
        [_shareButton setImage:[UIImage imageNamed:SHARE_IMAGE] forState:UIControlStateNormal];
        [_shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareButton;
}

#pragma mark -分享
- (void)shareButtonClick:(BaseButton *)button {
    if (!self.shareView) {
        self.shareView = [[ShareView alloc] initWithFrame:self.view.bounds];
        self.shareView.alpha = 0.0;
        [self.view addSubview:self.shareView];
        self.shareView.shareTitle = [NSString stringWithFormat:@"%@", self.tempModel.name];
        self.shareView.shareText = NSLocalizedString(@"分享内容", nil);
        self.shareView.shareUrl = [NSString stringWithFormat:@"%@", self.tempModel.cover];
        [UIView animateWithDuration:1 animations:^{
            self.shareView.alpha = 1.0;
        }];
    } else {
        [UIView animateWithDuration:1 animations:^{
            self.shareView.alpha = 1.0;
            
        }];
    }
    
}

#pragma mark - Method 设置拉伸效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
    //    NSLog(@"%g", yOffset);
    if (yOffset < 0) {
        self.myImageView.frame = CGRectMake(0, yOffset, SCREEN_WIDTH, -yOffset);
        //        imageView.alpha = -yOffset/1000*2+0.1;
        //        NSLog(@"%f", -yOffset/1000+0.7);
    }
}
#pragma mark - Method 每个分区返回的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
#pragma mark - Method 设置cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resue = @"dreamCityPrefectureTrailsArrayTableViewCell";
    JSMDreamCityPrefectureTrailsArrayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (!cell) {
        cell = [[JSMDreamCityPrefectureTrailsArrayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailLabel.text = self.dreamCityPrefectureTrailsArrayModel.detail;
    return cell;
}
#pragma mark - Method cell返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19], NSFontAttributeName, nil];
    CGRect rect = [self.dreamCityPrefectureTrailsArrayModel.detail boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    NSString *strURL = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/trail/%@?&api_version=1&app_version=4.3.3&trail_id=%@&device_type=2", self.tempModel.vid, self.tempModel.vid];
    NSLog(@"bsbsbsbssbsbbsb%@",strURL);
        [AFNetworkingTool getUrl:strURL hudSuperView:self.view result:JSON success:^(id result) {
            NSDictionary *dic = result;
            self.dreamCityPrefectureTrailsArrayModel = [[JSMDreamCityPrefectureTrailsArrayModel alloc] init];
            [self.dreamCityPrefectureTrailsArrayModel setValuesForKeysWithDictionary:dic];
            self.tempArray = self.dreamCityPrefectureTrailsArrayModel.picturesArray;
            self.title = [NSString stringWithFormat:@"%@ · %@↑", self.dreamCityPrefectureTrailsArrayModel.destination, self.dreamCityPrefectureTrailsArrayModel.name];
            self.scrollImageArray = self.dreamCityPrefectureTrailsArrayModel.picturesArray;
            [self.myImageView sd_setImageWithURL:[NSURL URLWithString:self.dreamCityPrefectureTrailsArrayModel.cover] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
            
            //        // 懒加载 分享按钮
            [self.shareButton setImage:[UIImage imageNamed:SHARE_IMAGE] forState:UIControlStateNormal];
            // 懒加载 收藏button
            self.collectButton.backgroundColor = [UIColor blackColor];
            ////        // 懒加载 路线详情button
            [self.moreImagesButton setImage:[UIImage imageNamed:IMAGEDISPLAY_IMAGE] forState:UIControlStateNormal];
            
            //        // tableView 刷新
            [self.myTableView reloadData];
        } failure:^(NSError *error) {
        }];
}
#pragma mark - Method 更多图片的按钮触发的方法
- (void)moreImageAction:(BaseButton *)button {
    NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:self.scrollImageArray.count];

    for (JSMDreamCityPrefectureTrailsPicturesModel *more in self.scrollImageArray) {
        NSURL *url = [NSURL URLWithString:more.picture];
        [imgArray addObject:url];
    }
    JSMDreamCityPrefectureImagesViewController *MoreVC = [[JSMDreamCityPrefectureImagesViewController alloc] initWithSourceData:imgArray withIndex:0];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:MoreVC];
    
    [self presentViewController:naVC animated:YES completion:nil];
    
    
}
#pragma mark 收藏
- (void)collectMessageButton:(BaseButton *)button{
    NSString *message;
    if ([button.imageView.image isEqual:[UIImage imageNamed:SAVE_ISHAVE_IMAGE]]) {
        [SaveTool cancelSaveModelInPlist:self.tempModel type:SaveDreamCityPrefectureModel];
        [button setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"取消收藏";
    }else{
        [SaveTool saveModelToPlist:self.tempModel type:SaveDreamCityPrefectureModel];
        [button setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"已收藏";
    }
    UIAlertController *alertControler = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertControler animated:YES completion:^{
    }];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
